﻿<Window x:Class="Bio.Views.Alignment.Options.OptionsWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:julmar="http://www.julmar.com/wpfhelpers" 
        xmlns:Controls="clr-namespace:Bio.Controls;assembly=Bio.Controls.ColorPicker" 
        WindowStartupLocation="CenterOwner" WindowStyle="SingleBorderWindow" ResizeMode="CanResize"
        Title="Options Window" Height="600" Width="800" Background="DarkGray">
    <Grid x:Name="LayoutRoot">
        
        <Grid.Resources>
            <julmar:KeyToResourceConverter x:Key="KeyToResourceConverter" Element="{Binding ElementName=LayoutRoot}" />
            
            <HierarchicalDataTemplate x:Key="optionTreeList" ItemsSource="{Binding Children}">
                <TextBlock x:Name="tb" Text="{Binding Name}" ToolTip="{Binding Description}" />
                <HierarchicalDataTemplate.Triggers>
                    <DataTrigger Binding="{Binding IsSelected}" Value="True">
                        <Setter TargetName="tb" Property="FontWeight" Value="Bold" />
                    </DataTrigger>
                </HierarchicalDataTemplate.Triggers>
            </HierarchicalDataTemplate>
            
            <DataTemplate x:Key="FontsAndColors">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition Width="2*" />
                    </Grid.ColumnDefinitions>

                    <Label VerticalAlignment="Center" Content="Header Font:" />
                    <ComboBox Grid.Column="1" Margin="4" VerticalAlignment="Center" ItemsSource="{Binding FontNames}" SelectedItem="{Binding SelectedFont}">
                        <ComboBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <VirtualizingStackPanel />
                            </ItemsPanelTemplate>
                        </ComboBox.ItemsPanel>
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding}" FontFamily="{Binding}" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>

                    <Label Grid.Row="1"  VerticalAlignment="Center" Content="Header Font Size:" />
                    <ComboBox Grid.Row="1" Grid.Column="1" Margin="4" VerticalAlignment="Center" 
                              ItemsSource="{Binding FontSizes}" SelectedItem="{Binding SelectedFontSize}" />

                    <Label Grid.Row="2" VerticalAlignment="Center" Content="Alignment Font:" />
                    <ComboBox Grid.Row="2" Grid.Column="1" Margin="4" VerticalAlignment="Center" ItemsSource="{Binding FixedFontNames}" SelectedItem="{Binding AlignmentFontFamily}">
                        <ComboBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <VirtualizingStackPanel />
                            </ItemsPanelTemplate>
                        </ComboBox.ItemsPanel>
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding}" FontFamily="{Binding}" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>

                    <Label Grid.Row="3"  VerticalAlignment="Center" Content="Alignment Font Size:" />
                    <ComboBox Grid.Row="3" Grid.Column="1" Margin="4" VerticalAlignment="Center" 
                              ItemsSource="{Binding FontSizes}" SelectedItem="{Binding AlignmentFontSize}" />

                    <GroupBox Grid.Row="4" Header="Colors" Margin="4" Grid.ColumnSpan="2">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width=".5*" />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>

                            <ListBox x:Name="colorOptions" ItemsSource="{Binding ColorOptions}">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <Rectangle Fill="{Binding Value}" Stroke="Black" StrokeThickness="1" Width="10" Height="10" />
                                            <TextBlock Margin="5,0" Text="{Binding Name}" />
                                        </StackPanel>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>

                            <Controls:ColorPicker Grid.Column="1" 
                                Color="{Binding ElementName=colorOptions, Path=SelectedItem.Value, Mode=TwoWay, PresentationTraceSources.TraceLevel=High}" />
                            
                        </Grid>
                    </GroupBox>

                    <CheckBox Grid.ColumnSpan="2" Grid.Row="5" Margin="4" VerticalAlignment="Center" Content="Show Row Numbers" IsChecked="{Binding ShowRowNumbers}" />

                </Grid>
            </DataTemplate>

            <DataTemplate x:Key="ReferenceSeqColors">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <ListBox x:Name="refColors" ItemsSource="{Binding ReferenceSequenceColors}" Margin="5">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Rectangle Fill="{Binding}" Stroke="Black" StrokeThickness="1" Height="25" Width="300"  />
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

                    <Grid Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Stretch">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Button Content="Remove _Color" Padding="10,5" Margin="5" Command="{Binding RemoveRefSeqColor}" CommandParameter="{Binding ElementName=refColors,Path=SelectedItem}" />
                        <Grid Grid.Column="1" Margin="5">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition Width="15" />
                            </Grid.ColumnDefinitions>
                            <ComboBox x:Name="refAllColors" ItemsSource="{Binding AllColors}" SelectedIndex="0"
                                      ScrollViewer.HorizontalScrollBarVisibility="Disabled" Grid.ColumnSpan="2" HorizontalAlignment="Stretch">
                                <ComboBox.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapPanel Width="300" />
                                    </ItemsPanelTemplate>
                                </ComboBox.ItemsPanel>
                                <ComboBox.ItemTemplate>
                                    <DataTemplate>
                                        <Rectangle Stroke="Black" StrokeThickness="1" Fill="{Binding}" Width="10" Height="10" ToolTip="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Center" />
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>
                            </ComboBox>
                            <Button Padding="10,5" Command="{Binding AddRefSeqColor}" CommandParameter="{Binding ElementName=refAllColors,Path=SelectedItem}">
                                <StackPanel Orientation="Horizontal">
                                    <Rectangle Stroke="Black" StrokeThickness="1" Fill="{Binding ElementName=refAllColors,Path=SelectedItem}" Width="50" Height="10" ToolTip="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Center" />
                                    <Label Margin="5" Content="{Binding ElementName=refAllColors,Path=SelectedItem}" />
                                </StackPanel>
                            </Button>
                        </Grid>
                    </Grid>
                </Grid>
            </DataTemplate>

            <DataTemplate x:Key="NucleotideColors">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    
                    <ListBox x:Name="nucList" Margin="5" Grid.RowSpan="2" ItemsSource="{Binding NucleotideColors}" SelectedIndex="0">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Margin="2">
                                    <Rectangle Stroke="Black" StrokeThickness="1" Fill="{Binding Color}" Margin="0,0,5,0" Width="20" Height="10" />
                                    <TextBlock Text="{Binding Symbol}" MinWidth="75" />
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

                    <Grid Grid.Column="1" Margin="5">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        
                        <Label Content="Symbol:" Margin="5" VerticalAlignment="Center" />
                        <TextBox Grid.Column="1" Text="{Binding ElementName=nucList, Path=SelectedItem.Symbol}" HorizontalAlignment="Stretch" VerticalAlignment="Center" MaxLength="1" />

                        <Label Grid.Row="1" Content="Color:" Margin="5" VerticalAlignment="Center" />
                        <ComboBox Grid.Row="1" Grid.Column="1" ItemsSource="{Binding AllColors}" SelectedItem="{Binding ElementName=nucList, Path=SelectedItem.Color}"
                                      ScrollViewer.HorizontalScrollBarVisibility="Disabled" Grid.ColumnSpan="2" HorizontalAlignment="Stretch">
                            <ComboBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel Width="300" />
                                </ItemsPanelTemplate>
                            </ComboBox.ItemsPanel>
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <Rectangle Stroke="Black" StrokeThickness="1" Fill="{Binding}" Width="10" Height="10" ToolTip="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Center" />
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                    </Grid>

                    <StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom">
                        <Button Content="_Add Symbol" Command="{Binding AddNucleotideColor}" Padding="10,5" Margin="5 "/>
                        <Button Content="_Remove Symbol" Command="{Binding RemoveNucleotideColor}" CommandParameter="{Binding ElementName=nucList,Path=SelectedItem}" Padding="10,5" Margin="5 "/>
                    </StackPanel>
                    
                </Grid>
            </DataTemplate>
            
            <DataTemplate x:Key="Grouping">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition Width="2*" />
                    </Grid.ColumnDefinitions>
                    
                    <Label VerticalAlignment="Center" Content="Grouping Minimum:" />
                    
                    <TextBox Grid.Column="1" VerticalAlignment="Center" MinWidth="75" Margin="5" Text="{Binding MinGroupingRange, UpdateSourceTrigger=PropertyChanged}">
                        <i:Interaction.Behaviors>
                            <julmar:NumericTextBoxBehavior AllowMouseDrag="True" />
                        </i:Interaction.Behaviors>
                    </TextBox>

                    <CheckBox Grid.Row="1" Grid.ColumnSpan="2" Margin="5" VerticalAlignment="Center" Content="Display Taxonomy Level in Group Header" IsChecked="{Binding ShowTaxonomyGroupLevel}" />
                    <CheckBox Grid.Row="2" Grid.ColumnSpan="2" Margin="5" VerticalAlignment="Center" Content="Sort by Name when not Grouped" IsChecked="{Binding SortByTaxonomy}" />
                    <CheckBox Grid.Row="3" Grid.ColumnSpan="2" Margin="5" VerticalAlignment="Center" Content="Always open with grouping if supported" IsChecked="{Binding OpenWithGrouping}" />
                </Grid>
            </DataTemplate>
            
        </Grid.Resources>
        
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" MinWidth="25" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <TreeView Margin="10" ItemTemplate="{StaticResource optionTreeList}" ItemsSource="{Binding Menu}" BorderBrush="Black" BorderThickness="1">
            <TreeView.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="True" />
                    <Setter Property="Foreground" Value="White" />
                    <Setter Property="IsSelected" Value="{Binding IsSelected}" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsSelected}" Value="True">
                            <Setter Property="FontWeight" Value="Bold" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>
        </TreeView>
        
        <ContentControl Grid.Column="1" Background="White" Margin="10" Content="{Binding}"
                        ContentTemplate="{Binding ResourceKey, Converter={StaticResource KeyToResourceConverter}, FallbackValue={StaticResource FontsAndColors}}" />

        <StackPanel Grid.Row="1" Grid.Column="1">
            <Separator />
            <Grid Margin="10,0,0,5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>

                <Button Content="OK" Grid.Column="0" Margin="5" Padding="5,0" IsDefault="True" MinHeight="25" Command="{Binding Save}">
                    <i:Interaction.Behaviors>
                        <julmar:CloseDialogBehavior />
                    </i:Interaction.Behaviors>
                </Button>
                <Button Content="Cancel" Grid.Column="1" Margin="5" Padding="5,0" IsCancel="true" />
                <Button Content="Reset" Grid.Column="2" Margin="5" Padding="5,0" Command="{Binding Reset}" />
            </Grid>
        </StackPanel>
        
    </Grid>
</Window>
